package com.topvision.camerasdk.camera;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.topvision.camerasdk.R;
import com.topvision.commomsdk.TouchInterface;
import com.topvision.commomsdk.uitl.AndroidUtils;
import com.topvision.commomsdk.uitl.PreferencesUtils;
import com.topvision.commomsdk.view.TvViewPager;
import com.topvision.voicesdk.BaseVoiceActivity;
import com.topvision.voicesdk.CommonVoice;
import com.topvision.voicesdk.VoiceInterface;

import java.util.ArrayList;


/**
 * 相册分辨率设置
 *
 * @author qijiahai
 */
public class SettingActivity extends BaseVoiceActivity implements TouchInterface, VoiceInterface {
    private TvViewPager viewPager;
    private View view1, view2, view3, view4;
    private ArrayList<View> viewList;
    TextView voiceTips;
    Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_setting);
        //hiddenNavigator();
        mContext = this;
        initView();
    }

    private void initView() {
        viewPager = (TvViewPager) findViewById(R.id.viewpager);
        viewPager = (TvViewPager) AndroidUtils.setAnimation(viewPager);
        viewPager.setNoScroll(true);
        //设置语音提示是否显示
        voiceTips = (TextView) findViewById(R.id.setting_voice_tips);
        if (!getIntent().getBooleanExtra(CameraConstants.VOICE_TIPS_FLAG, false)) {
            voiceTips.setVisibility(View.GONE);
        }
        LayoutInflater inflater = getLayoutInflater();
        view1 = inflater.inflate(R.layout.setting_itme, null);
        view2 = inflater.inflate(R.layout.setting_itme1, null);
        view3 = inflater.inflate(R.layout.setting_itme2, null);
        view4 = inflater.inflate(R.layout.setting_itme3, null);
        viewList = new ArrayList<View>();// 将要分页显示的View装入数组中
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
        viewList.add(view4);
        viewPager.setAdapter(pagerAdapter);
        showCurrentPicSize();


    }

    /**
     * 显示当前用户设置的分辨率
     * create at 2017/2/7 10:22
     */
    private void showCurrentPicSize() {
        int width = PreferencesUtils.getInt(mContext, "wide", CameraActivity.DEFAULT_PICTURE_WIDTH);
        switch (width) {
            case 1024:
                viewPager.setCurrentItem(0);
                break;
            case 1600:
                viewPager.setCurrentItem(1);
                break;
            case 2592:
                viewPager.setCurrentItem(2);
                break;
            case 3264:
                viewPager.setCurrentItem(3);
                break;

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        setTouchInterfaceListener(this);
        setVoiceInterfaceListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        setTouchInterfaceListener(null);
        setVoiceInterfaceListener(null);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    @Override
    public void doForwardTouch() {
        int currentItem = viewPager.getCurrentItem();
        viewPager.setCurrentItem(currentItem + 1);

    }

    @Override
    public void doBackwardTouch() {
        int currentItem = viewPager.getCurrentItem();
        viewPager.setCurrentItem(currentItem - 1);
    }

    @Override
    public void doClickTouch() {
        if (viewPager == null) {
            return;
        }
        int currentItem = viewPager.getCurrentItem();
        choose(currentItem);

    }

    @Override
    public void doDoubleTouch() {
    }

    public void choose(int i) {
        switch (i) {
            case 0:
                PreferencesUtils.putInt(SettingActivity.this, "wide", 1024);
                PreferencesUtils.putInt(SettingActivity.this, "high", 768);
                break;
            case 1:
                PreferencesUtils.putInt(SettingActivity.this, "wide", 1600);
                PreferencesUtils.putInt(SettingActivity.this, "high", 1200);
                break;
            case 2:
                PreferencesUtils.putInt(SettingActivity.this, "wide", 2592);
                PreferencesUtils.putInt(SettingActivity.this, "high", 1944);
                break;
            case 3:
                PreferencesUtils.putInt(SettingActivity.this, "wide", 3264);
                PreferencesUtils.putInt(SettingActivity.this, "high", 2448);
                break;
        }
        finish();
    }


    PagerAdapter pagerAdapter = new PagerAdapter() {
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getCount() {
            return viewList.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position,
                                Object object) {
            container.removeView(viewList.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewList.get(position));
            return viewList.get(position);
        }
    };

    @Override
    public void dealWithVoice(String data) {
        data = data.toLowerCase();

        if (CommonVoice.VOICE_ZH_BACK.equals(data) || CommonVoice.VOICE_US_BACK.equalsIgnoreCase(data)) {
            this.finish();
        } else if (CommonVoice.VOICE_ZH_UP.equals(data) || CommonVoice.VOICE_US_UP.equalsIgnoreCase(data)) {
            doBackwardTouch();
        } else if (CommonVoice.VOICE_ZH_DOWN.equals(data) || CommonVoice.VOICE_US_DOWN.equalsIgnoreCase(data)) {
            doForwardTouch();
        } else if (CommonVoice.VOICE_ZH_OK.equals(data) || CommonVoice.VOICE_US_OK.equalsIgnoreCase(data) || CommonVoice.VOICE_US_CONFIRM.equalsIgnoreCase(data)) {
            doClickTouch();
        }

    }

}
